package com.ifnk.portaljava.hlyt.etl.beans;

import com.ifnk.portaljava.dapWeb.advance.DBAccess;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * <p>Title:Gn数据类 </p>
 *
 * <p>Description: Gn数据Bean，包含将数据插入数据库的方法</p>
 *
 * <p>Copyright: Copyright (c) 2010</p>
 *
 * <p>Company: </p>
 * 
 * @author chengzhang.shi.hlyt@gmail.com
 * @version 1.0
 */
public class GnData {

	private String time;
	private int createSuccessNo;
	private int createRequestNo;
	private int deleteSuccessNo;
	private int deleteRequestNo;
	private double upPackageRate;
    private double upByteRate;
    private double downPackageRate;
    private double downByteRate;
    
    private double createSuccessPcnt;
    private double deleteSuccessPcnt;
    
    private String data;
    
    public boolean setData(String data) {
		if(checkData(data)) {
			this.data = data;
			String[] datas = data.split(",");
			time = datas[0];
			createSuccessNo = getInt(datas[1]);
			createRequestNo = getInt(datas[2]);
			deleteSuccessNo = getInt(datas[3]);
			deleteRequestNo = getInt(datas[4]);
			
			upPackageRate = getDouble(datas[5]);
			upByteRate = getDouble(datas[6]);
			downPackageRate = getDouble(datas[7]);
			downByteRate = getDouble(datas[8]);
			
			if(createRequestNo == 0) {
				createSuccessPcnt = 0;
			} else {
				createSuccessPcnt = 1.0 * createSuccessNo / createRequestNo;
			}
			
			if(deleteRequestNo == 0) {
				deleteSuccessPcnt = 0;
			} else {
				deleteSuccessPcnt = 1.0 * deleteSuccessNo / deleteRequestNo;
			}
			
			return true;
		}
		return false;
	}
	
	public boolean checkData(String data) {
		return data != null && data.split(",").length == 9;
	}
	
	private int getInt(String data) {
		int num = 0;
		try {
			num = Integer.parseInt(data);
		} catch (NumberFormatException e) {
			
		}
		return num;
	}
	
	private double getDouble(String data) {
		double num = 0;
		try {
			num = Double.parseDouble(data);
		} catch (NumberFormatException e) {
			
		}
		return num;
	}
	
	public boolean insert() {
		Connection conn = DBAccess.getConn();
		Log loger = LogFactory.getLog(GnData.class);
		Statement statement;
		String sql;
		try {
			conn.setAutoCommit(false);
		} catch (SQLException e) {
			loger.error("设置手动提交失败！");
			e.printStackTrace();
		}
		try {
			statement = conn.createStatement();
			String tableName = "gnsuccess";
			sql = "delete from " + tableName + " where time = " + time;
			statement.executeUpdate(sql);
			sql = "insert into " + tableName + " values (null,";
			sql += time + "," + createSuccessNo + "," + createRequestNo + "," + deleteSuccessNo + "," + deleteRequestNo;
			sql += ",now())";
			statement.executeUpdate(sql);

			tableName = "gnflow";
			sql = "delete from " + tableName + " where time = " + time;
			statement.executeUpdate(sql);
			sql = "insert into " + tableName + " values (null,";
			sql += time + "," + upPackageRate + "," + upByteRate + "," + downPackageRate + "," + downByteRate;
			sql += ",now())";
			statement.executeUpdate(sql);
			
			conn.commit();
			loger.info("导入数据库成功！数据为：" + data);
		} catch (SQLException e) {
			loger.error("导入数据库失败！数据为：" + data);
			try {
				conn.rollback();
			} catch (SQLException e1) {
				loger.error("回滚失败！");
				e1.printStackTrace();
			}
			return false;
		} finally {
			
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e) {
				loger.error("设置自动提交失败！");
				e.printStackTrace();
			}
			DBAccess.freeConnection(conn);
		}
		return true;
	}
    
	public String getTime() {
		return time;
	}
	public void setTime(String time) {
		this.time = time;
	}
	public int getCreateSuccessNo() {
		return createSuccessNo;
	}
	public void setCreateSuccessNo(int createSuccessNo) {
		this.createSuccessNo = createSuccessNo;
	}
	public int getCreateRequestNo() {
		return createRequestNo;
	}
	public void setCreateRequestNo(int createRequestNo) {
		this.createRequestNo = createRequestNo;
	}
	public int getDeleteSuccessNo() {
		return deleteSuccessNo;
	}
	public void setDeleteSuccessNo(int deleteSuccessNo) {
		this.deleteSuccessNo = deleteSuccessNo;
	}
	public int getDeleteRequestNo() {
		return deleteRequestNo;
	}
	public void setDeleteRequestNo(int deleteRequestNo) {
		this.deleteRequestNo = deleteRequestNo;
	}
	public double getUpPackageRate() {
		return upPackageRate;
	}
	public void setUpPackageRate(double upPackageRate) {
		this.upPackageRate = upPackageRate;
	}
	public double getUpByteRate() {
		return upByteRate;
	}
	public void setUpByteRate(double upByteRate) {
		this.upByteRate = upByteRate;
	}
	public double getDownPackageRate() {
		return downPackageRate;
	}
	public void setDownPackageRate(double downPackageRate) {
		this.downPackageRate = downPackageRate;
	}
	public double getDownByteRate() {
		return downByteRate;
	}
	public void setDownByteRate(double downByteRate) {
		this.downByteRate = downByteRate;
	}
	public double getCreateSuccessPcnt() {
		return createSuccessPcnt;
	}
	public void setCreateSuccessPcnt(double createSuccessPcnt) {
		this.createSuccessPcnt = createSuccessPcnt;
	}
	public double getDeleteSuccessPcnt() {
		return deleteSuccessPcnt;
	}
	public void setDeleteSuccessPcnt(double deleteSuccessPcnt) {
		this.deleteSuccessPcnt = deleteSuccessPcnt;
	}
    
    
}
